//
//  HLBSalesStatisticsVC.m
//  dyseller
//
//  Created by 红喇叭 on 2017/12/7.
//  Copyright © 2017年 红喇叭. All rights reserved.
//

#import "HLBSalesStatisticsVC.h"

@interface SalesCell:UITableViewCell

@property(nonatomic,strong)UILabel *shopName;
@property(nonatomic,strong)UILabel *salceNum;
@property(nonatomic,strong) UIImageView *topImg;

@end

@implementation SalesCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        _shopName = [[UILabel alloc] init];
        _shopName.textColor = UIColorFromRGB(0x333333);
        _shopName.font = font14;
        [self.contentView addSubview:_shopName];
        _shopName.sd_layout.leftSpaceToView(self.contentView, GETWITH(30)).topSpaceToView(self.contentView, 30).widthIs(GETWITH(300)).autoHeightRatio(0);
        [_shopName setMaxNumberOfLinesToShow:2];
        
        _salceNum = [[UILabel alloc] init];
        _salceNum.font = font15;
        _salceNum.textAlignment = NSTextAlignmentCenter;
        _salceNum.textColor = _shopName.textColor;
        [self.contentView addSubview:_salceNum];
        _salceNum.sd_layout.leftSpaceToView(_shopName,GETWITH(170))
        .centerYEqualToView(_shopName).heightIs(20).maxWidthIs(GETWITH(120));
        
        _topImg = [[UIImageView alloc] init];
        [self.contentView addSubview:_topImg];
        _topImg.sd_layout.topSpaceToView(self.contentView, 0).leftSpaceToView(self.contentView, GETWITH(30)).widthIs(GETWITH(26)).heightIs(20);

    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
    CGContextFillRect(context, rect);
    
    CGContextSetStrokeColorWithColor(context,UIColorFromRGB(0xdddddd).CGColor);
    CGContextStrokeRect(context, CGRectMake(0, rect.size.height - 0.1, rect.size.width, 0.1));
}

@end

@interface HLBSalesStatisticsVC ()<UITableViewDelegate,UITableViewDataSource>{
    BOOL  isenumerator;
    NSMutableArray *_btnArray;
}

@property(nonatomic,strong)UITableView *salesTableView;
@property(nonatomic,strong)NSMutableArray *salceArray;

@end

@implementation HLBSalesStatisticsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavTitle:@"销售统计" titleColor:kBlackColor font:font17];
    [self.view addSubview:self.salesTableView];
    self.salesTableView.tableHeaderView = [self tableHeadView];
    [self getGoodsSales:@"4"];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   
}

/**
 请求商品销量数据

 @param flag 1:本天 2:本周 3:本月 4：统计 
 */
- (void)getGoodsSales:(NSString *)flag{
   
    isenumerator = NO;
    __weak typeof (self)weakself = self;
    [[PPHttpRequsetData shareInstance] requestStyle:Request_POST URL:@"/ShopApi/Analyze/goodsSales" isactive:NO parameter:@{@"flag":flag,@"sort":@"DESC"} successBlock:^(PPHttpRequest *request, NSArray* responseObj) {
        _salceArray = [responseObj mutableCopy];
        [weakself updateSelectState:[flag intValue]];
        [weakself.salesTableView reloadData];
    } failBlock:^(PPHttpRequest *request, NSString *errMsg) {
        [AlertUtils showToastWithMessage:errMsg inView:self.view hideAfterDelay:2.0 beIndeter:NO];
    }];

}
- (void)updateSelectState:(int)row{
    for (UIButton *btn in _btnArray) {
        if(btn.tag == row){
            btn.selected = YES;
        }else{
            btn.selected = NO;
        }
    }
}

#pragma  mark ========懒加载========
- (UITableView *)salesTableView{
    if(_salesTableView == nil){
        _salesTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H-TabBarItemHeight-NavigationHeight+49) style:UITableViewStylePlain];
        _salesTableView.dataSource = self;
        _salesTableView.delegate = self;
        _salesTableView.estimatedRowHeight = 60;
        
        _salesTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, 0.1)];
        [_salesTableView registerClass:[SalesCell class] forCellReuseIdentifier:@"SalesCell"];
        _salesTableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    }
    return _salesTableView;
}

- (UIView *)tableHeadView{
    UIView *headV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, GETHEIGHT(120))];
    headV.backgroundColor = kWhiteColor;
    NSArray *array = @[@"今日",@"本周",@"本月",@"统计"];
    _btnArray = @[].mutableCopy;
    for(int i=0;i<4;i++){
        UIButton *btn = [[UIButton alloc] init];
        btn.frame = CGRectMake(GETWITH(30)+GETWITH(180)*i, 0, GETWITH(154), GETHEIGHT(54));
        btn.centerY = headV.centerY;
        [btn setTitle:array[i] forState:UIControlStateNormal];
        btn.tag = i+1;
        btn.titleLabel.font = font14;
        [btn setTitleColor:kBlackColor forState:UIControlStateNormal];
        [btn setTitleColor:kWhiteColor forState:UIControlStateSelected];
        [btn setBackgroundImage:[UIImage imageNamed:@"sale_bg1_default"] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageNamed:@"sale_bg2_default"] forState:UIControlStateSelected];
        if(i==3) btn.selected = YES;
        [btn addTarget:self action:@selector(salceBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [headV addSubview:btn];
        [_btnArray addObject:btn];
    }
    
    return headV;
}

- (void)salceBtnAction:(UIButton *)sender{
   // sender.selected = !sender.selected;
    [self getGoodsSales:[NSString stringWithFormat:@"%ld",sender.tag]];
}

#pragma  mark ========UITableViewDelegate,UITableViewDataSource========
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _salceArray.count;
}

/*{
    "id": "45824",
    "goods_name": "韭菜猪红",
    "goods_price": "5.00",
    "goods_image": "https://img.honglaba.com/Static/Uploads/Goods_pic/2017-09-27/59cb588728a5b.jpg",
    "order_total": "0"
},*/

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dic = _salceArray[indexPath.row];
    SalesCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SalesCell" forIndexPath:indexPath];
    cell.shopName.text = dic[@"goods_name"];
    cell.salceNum.text = dic[@"order_total"];
    
    if(indexPath.row == 0){
        cell.topImg.image = [UIImage imageNamed:@"sale_bg8_default"];
    }else if(indexPath.row == 1){
        cell.topImg.image = [UIImage imageNamed:@"sale_bg9_default"];
    }else if(indexPath.row == 2){
        cell.topImg.image = [UIImage imageNamed:@"sale_bg10_default"];
    }else{
        cell.topImg.image = nil;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dic = _salceArray[indexPath.row];
    CGSize titleSize = [dic[@"goods_name"] boundingRectWithSize:CGSizeMake(GETWITH(300), MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font14} context:nil].size;
    if(titleSize.height<20) return 50 + GETWITH(30);
    else return 50 + GETWITH(60);
    return 0;
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, 50)];
    
    [self viewForHeaderInSection:view];
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 50;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    return [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, 0.1)];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1;
}


- (void)viewForHeaderInSection:(UIView *)view{
    
    UIImageView *baseImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"sale_bg3_default"]];
    baseImg.frame = view.bounds;
    [view addSubview:baseImg];
    
    UILabel *shopName = [[UILabel alloc] init];
    shopName.textColor = UIColorFromRGB(0x333333);
    shopName.text = @"商品名称";
    shopName.textAlignment = NSTextAlignmentCenter;
    shopName.font = font14;
    [view addSubview:shopName];
    shopName.sd_layout.leftSpaceToView(view, 0).centerYEqualToView(view).widthIs((view.width/2-5)).heightIs(20);
    
    UIView *horView = [[UIView alloc] init];
    horView.backgroundColor = kBlackColor;
    [view addSubview:horView];
    horView.sd_layout.leftSpaceToView(shopName, 4).topSpaceToView(view, 5).bottomSpaceToView(view, 5).widthIs(0.5);
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:[UIImage imageNamed:@"sale_bg4_default_click"] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"sale_bg4_default"] forState:UIControlStateSelected];
    [view addSubview:btn];
    btn.sd_layout.leftSpaceToView(horView, 4).centerYEqualToView(shopName)
    .heightIs(30).rightSpaceToView(view, 0);
    [btn addTarget:self action:@selector(enumerator:) forControlEvents:UIControlEventTouchUpInside];
    
    btn.selected = isenumerator;
    
}

- (void)enumerator:(UIButton *)btn{
    isenumerator = !isenumerator;
    _salceArray = (NSMutableArray *)[[_salceArray reverseObjectEnumerator] allObjects];
    [_salesTableView reloadData];
}

@end
